var nodemailer = require('nodemailer');
var fs = require('fs');
var transporter = nodemailer.createTransport({
  service: 'QQ',
  auth: {
    user: '1634566606@qq.com',//发送者邮箱
    pass: '' //邮箱第三方登录授权码
  },
  debug: true
}, {
    from: '1634566606@qq.com',//发送者邮箱
    headers: {
      'X-Laziness-level': 1000
    }
  });
var message = {};
exports.send = function ({ address, name, type }) {
  if (type == true) {
    fs.readFile(__dirname + '/temp/avue.html', 'utf8', function (err, data) {
      var html = data;
      var qq = address.replace('@qq.com', '');
      html = html.replace('${username}', qq);
      html = html.replace('${username}', qq);
      html = html.replace('${password}', '123456')
      message = {
        // Comma separated lsit of recipients 收件人用逗号间隔
        to: address,

        // Subject of the message 信息主题
        subject: 'avue：欢迎购买使用，请查收',

        // plaintext body
        text: '',

        // Html body
        html: html,

        // Apple Watch specific HTML body 苹果手表指定HTML格式
        watchHtml: html,

        // An array of attachments 附件
        attachments: []

      };
      console.log('SMTP Configured');
      send()
    })
  } else {
    message = {
      // Comma separated lsit of recipients 收件人用逗号间隔
      to: address,

      // Subject of the message 信息主题
      subject: name + '——avue授权书',

      // plaintext body
      text: '授权书详情见附件，感谢你对avue的支持',

      // Html body
      html: '',

      // Apple Watch specific HTML body 苹果手表指定HTML格式
      watchHtml: '',

      // An array of attachments 附件
      attachments: [
        {
          filename: name + '.jpg',
          path: __dirname + '/../file/' + name + '.jpg',
          cid: '00001'  // should be as unique as possible 尽可能唯一
        }
      ]

    };
    console.log('SMTP Configured');
    send()
  }
}

function send () {
  console.log('Send Mail');
  transporter.sendMail(message, (error, info) => {
    if (error) {
      console.log('Error occurred');
      console.log(error.message);
      return;
    }
    console.log('Message sent successfully!');
    console.log('Server responded with "%s"', info.response);
    transporter.close();
  });
}